const Models = require('../../models')

class Leasingmanagements{
    static async addLease(ctx){
        const req = ctx.request.body;
        const { userId, carId, time, money} = req;
        if(req.userId && req.carId && req.time&& req.money){
            try{
                await Models.leasingmanagements.create({
                    userId,
                    carId,
                    time,
                    money,
                    createdAt: new Date(),
                    updatedAt: new Date(),
                })
                await Models.cars.update({isUse: false},{
                    where: {
                        id: req.carId
                    }
                })
                ctx.response.status = 200;
                ctx.response.body = {
                    code: 200,
                    msg: '创建成功'
                }
            }catch (e) {
                ctx.response.status = 200;
                ctx.response.body = {
                    code: 516,
                    msg: '创建失败'
                }
            }
        }else{
            ctx.response.status = 200;
            ctx.response.body = {
                code: 416,
                msg: '数据不完整'
            }
        }
    }

    static async getUserLease(ctx){
        const req = ctx.request.body;
        const leasesData =await Models.leasingmanagements.findAll({
            where:{
                userId: req.userId
            }
        });
        let userResults = await Promise.all(leasesData.map(async (item) => {
            // 等待异步操作完成，返回执行结果
            const userdata = await Models.users.findOne({
                where: {
                    id: item.userId
                }
            })
            return {userId: item.userId,carId: item.carId,time: item.time,money: item.money,identity: userdata.identity,name: userdata.name,iphone: userdata.iphone};
        }));
        let results = await Promise.all(userResults.map(async (item) => {
            const carData = await Models.cars.findOne({
                where: {
                    id: item.carId
                }
            })
            return {...item,carNumber: carData.carNumber}
        }))
        ctx.response.status = 200;
        ctx.response.body = {
            code: 200,
            msg: '数据请求成功',
            data: results
        }
    }

    static async getLease(ctx){
        const leaseData =await Models.leasingmanagements.findAll();
        let results = await Promise.all(leaseData.map(async (item) => {
            // 等待异步操作完成，返回执行结果
            const userdata = await Models.users.findOne({
                where: {
                    id: item.userId
                }
            })
            return {userId: item.userId,carId: item.carId,time: item.time,money: item.money,identity: userdata.identity,name: userdata.name,iphone: userdata.iphone};
        }));
        let lastResults = await Promise.all(results.map(async (item) => {
            const carData = await Models.cars.findOne({
                where: {
                    id: item.carId
                }
            })
            return {...item,carNumber: carData.carNumber}
        }))
        ctx.response.status = 200;
        ctx.response.body = {
            code: 200,
            msg: '数据请求成功',
            data: lastResults
        }
    }

    static async deleteLease(ctx){
        const req = ctx.request.body;
        const {userId,carId} = req;
        try{
            await Models.leasingmanagements.destroy({
                where: {
                    userId,
                    carId
                }
            })
            await Models.cars.update({isUse: true},{
                where: {
                    id: carId
                }
            })
            ctx.response.status = 200;
            ctx.response.body = {
                code: 200,
                msg: '请求成功'
            }
        }catch (e) {
            ctx.response.status = 200;
            ctx.response.body = {
                code: 416,
                msg: '请求失败'
            }
        }
    }
}

module.exports = Leasingmanagements